package com.dangf.app.launcher.service

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.support.v7.widget.RecyclerView
import com.dangf.app.launcher.app.V
import com.dangf.app.launcher.dialog.OutboardMotorDialog
import com.dangf.app.launcher.dialog.SystemSettingDialog
import com.dangf.app.launcher.entity.Soft
import com.dangf.app.launcher.entity.event.IOEType
import com.dangf.app.launcher.entity.event.IOEvent
import com.dangf.app.launcher.entity.event.MainEType
import com.dangf.app.launcher.entity.event.MainEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import pw.hais.utils_lib.app.AppBaseActivity
import pw.hais.utils_lib.utils.L
import pw.hais.utils_lib.utils.ShellUtils

/**
 * Created by hais1992 on 2017/9/10/010.
 */

class EventService : Service() {
    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        stopSelf()
    }

    //订阅事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(mainEventEntity: MainEvent) {
        L.e("收到 MAIN 事件：" + mainEventEntity.type)
        when (mainEventEntity.type) {
            MainEType.BrightnessAdjustmentWindows -> {
                SystemSettingDialog(mainEventEntity.obj as AppBaseActivity).show()
            }
            MainEType.OutboardMotorWindows -> {
                OutboardMotorDialog(mainEventEntity.obj as AppBaseActivity).show()
            }
            MainEType.ShowToast -> L.showShort(mainEventEntity.obj.toString())
            MainEType.RefreshAppList -> {
                val view = mainEventEntity.obj as RecyclerView
                view.adapter.notifyItemChanged(view.tag as Int)
            }
        }
    }


    //订阅事件
    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    fun onEventMainThreadx(event: IOEvent) {
        L.e("收到 BACKGROUND 事件：" + event.type)
        when (event.type) {
            IOEType.CleanGameData -> {
                val sb = StringBuffer()
                V.mAppList?.forEach {
                    sb.append("rm -rf /data/data/${it.packageName}/databases/WEGAMEDB2* && rm -rf /data/data/${it.packageName}/app_webview/Cookies* &&")
                }

                sb.append(" am force-stop com.tencent.mobileqq && rm -rf /data/data/com.tencent.mobileqq/databases /data/data/com.tencent.mobileqq/files /data/data/com.tencent.mobileqq/shared_prefs ")   //删除腾讯QQ登陆记录

                ShellUtils.execCmd(sb.toString(), true)
                MainEvent.post(MainEType.ShowToast, "登陆数据已清空!")
            }
            IOEType.InstallApk -> {
                val map = event.obj as Map<*, *>
                val item = map["Data"] as Soft
                val view = map["View"] as RecyclerView
                ShellUtils.execCmd("pm install -r ${item.apkUrl}", true)
                MainEvent.post(MainEType.ShowToast, " ${item.name} 安装成功!")
                MainEvent.post(MainEType.RefreshAppList, view)
            }
        }
    }
}
